package com.pt.iwebshop.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pt.iwebshop.model.EbNews;
import com.pt.iwebshop.model.EbProduct;
import com.pt.iwebshop.model.EbProductCategory;
import com.pt.iwebshop.service.EbNewsService;
import com.pt.iwebshop.service.EbProductCategorySercie;
import com.pt.iwebshop.service.EbProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

//第一个迎宾 ：证
@Controller//迎宾证
@RequestMapping("/front")//工号
public class IndexController {

	@Autowired
	private EbProductService ebProductService;

	@Autowired
	private EbNewsService ebNewsService;

	@Autowired
	private EbProductCategorySercie ebProductCategorySercie;


	//行为 返回首页的数据(model)和视图(view-html)
	@RequestMapping("/index")//返回前端首页的数据和视图
	public String index(Model model, HttpSession session,
						@RequestParam(value = "pSize",defaultValue = "1") Integer pSize,
						@RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){
		//1.调用service层查询分页的商品信息
		Page<EbProduct> ebProductPage = ebProductService.selectList(pSize, pageSize);
		//2.调用service层查询分页的新闻信息
		List<EbNews> ebNewsList = ebNewsService.selectList(1, 7);


		//3.调用service层查询商品分类信息
		Map<String, List<EbProductCategory>> categoryMap = ebProductCategorySercie.selectBigAndSmallCate();
		//放到session域中
		//session.setAttribute("categoryMap",categoryMap);


		//4.调用service查询商品的最大页数
		int maxPage = ebProductService.getMaxPage(pageSize);

		//4.将数据绑定model中  key-value进行数据绑定
		model.addAttribute("ebProductList",ebProductPage.getRecords());
		model.addAttribute("ebNewsList",ebNewsList);
		model.addAttribute("categoryMap",categoryMap);

		//绑定当前页
		model.addAttribute("curPage",pSize);
		//是否有下一页  ebProductPage.hasNext()//判断是否还有下一页
		model.addAttribute("ebProductPage",ebProductPage);
		//绑定最大页数
		model.addAttribute("maxPage",maxPage);
		//5.返回视图(html)
		return "index";//templates/index.html

	}




}
